package IEFYP.JunkCallFiltering;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import IEFYP.JunkCallFiltering.MyAdapter;
public class MyAdapter extends SimpleAdapter {
    
    Map<Integer, Boolean> map; 
    
    LayoutInflater mInflater;
    
    private List<Map<String,String>> mList;
    private String[] from;
    private int[] to;
    private Context context;
    
    private int resource;
    private int res_checkbox;
    public boolean AllChecked;
    
    public void SetCheckboxResource(int resourceID){
    	res_checkbox = resourceID;
    }
    

    
    public void refreshCheckBox(int position){
    	for(int i = 0; i < map.size(); i++) {   		
    			map.put(i, false);   
    	} 
    }
    
    public MyAdapter(Context context, List<Map<String, String>> data,
                    int resource, String[] from, int[] to,boolean AllChecked) {
            super(context, data, resource, from, to);
            
            this.from = from;     
            this.to = to;
            this.resource = resource;
            this.AllChecked = AllChecked;
            this.context = context;
            
            
            res_checkbox =0;
            
            map = new HashMap<Integer, Boolean>();
            mInflater = LayoutInflater.from(context);
            
           // mList = data;
            mList = new ArrayList<Map<String, String>>();
            for(Map<String,String> a: data ){    
            	mList.add(a);
            }
            
            for(int i = 0; i < data.size(); i++) {
                    map.put(i, AllChecked);                  //for storing information of checkbox
            } 
    }
    
    public MyAdapter(Context context, List<Map<String, String>> data,
            int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
    this.from = from;
    this.to = to;
    this.resource = resource;

    
    res_checkbox =0;
    
    map = new HashMap<Integer, Boolean>();
    mInflater = LayoutInflater.from(context);
   // mList = data;
    mList = new ArrayList<Map<String, String>>();
    for(Map<String,String> a: data ){    
    	mList.add(a);
    }
    for(int i = 0; i < data.size(); i++) {
            map.put(i, false);                  //for storing information of checkbox
    } 
}
    
    public boolean checkSelected(){
    	
    	for(int i=0; i < map.size(); i++){
    		if(map.get(i)){
    			Log.v("map"+i, ""+map.get(i));
    			return true;			
    		}   		
    	}
    	return false;
    	
    	
    }
    
    @Override
    public int getCount() {
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
            return position;
    }

    

    public List<Map<String,String>> getList() {
            return mList; 
    }

     public MyAdapter clone(){ 
    	MyAdapter a = new MyAdapter(context, mList,resource,from,to);
    	a.map.clear();
    
    	a.map.putAll(this.map);
    	
    	return a;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	
            if(convertView == null) {
                    convertView = mInflater.inflate(resource, null);
            }
            
            for(int i=0;i< from.length ; i++){
           TextView tN = (TextView) convertView.findViewById(to[i]);
            tN.setText((String)mList.get(position).get(from[i]));
            
            }
            CheckBox checkBox;
            if(res_checkbox==0){
            	checkBox = (CheckBox) convertView.findViewById(R.id.b_select);
            }else{
            	checkBox = (CheckBox) convertView.findViewById(res_checkbox);
            }
            
            	checkBox.setChecked(map.get(position));  

            
            
            return convertView;
    }
    
}
